package com.zzh.test1;

/**
 * Object实现唤醒线程 阻塞线程
 */
public class Test2 {
    static Object objLock = new Object();

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            synchronized (objLock) {// 必须要持有锁才能调用wait方法 否则会抛出IllegalMonitorStateException异常
                try {
                    objLock.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("t1 is waken up");
            }
        }, "t1");
        t1.start();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }


        Thread t2 = new Thread(() -> {
            synchronized (objLock){ //必须要持有锁才能调用notify方法 否则会抛出IllegalMonitorStateException异常
                objLock.notify();//唤醒t1线程
                System.out.println("t2 is notifying t1");
            }
        });
        t2.start();

        //输出结果：
        //t2 is notifying t1
        //t1 is waken up

    }
}
